package com.woke.dao;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.List;

public class ChouJiang {
	private int id;
	private String cjname;
	private String cjcreatetime;
	private String cjupdatetime;
	
	public String getCjcreatetime() {
		return cjcreatetime;
	}
	public void setCjcreatetime(String cjcreatetime) {
		this.cjcreatetime = cjcreatetime;
	}
	public String getCjupdatetime() {
		return cjupdatetime;
	}
	public void setCjupdatetime(String cjupdatetime) {
		this.cjupdatetime = cjupdatetime;
	}
	public String getCjname() {
		return cjname;
	}
	public void setCjname(String cjname) {
		this.cjname = cjname;
	}
	public int getId() {
		return id;
	}
	public void setId(int id) {
		this.id = id;
	}
	
	
	
	public ChouJiang() {

	}
	
	public ChouJiang(int id, String cjname,String cjcreatetime,String cjupdatetime) {
		super();
		this.id = id;
		this.cjname = cjname;
		this.cjcreatetime= cjcreatetime;
		this.cjupdatetime = cjupdatetime;
	}

	public static List<ChouJiang> getData(String sql) {
		Connection con = null;
		Statement st = null;
		ResultSet rs = null;
		List<ChouJiang> list = new ArrayList<ChouJiang>();
		try {
			
			con = WokeConnection.getConnection();
			st = con.createStatement();
			rs = st.executeQuery(sql);
			while (rs.next()) {
				int id = rs.getInt("id");
				String cjname = rs.getString("cjname");
				String cjcreatetime = rs.getString("cjcreatetime");
				String cjupdatetime = rs.getString("cjupdatetime");
				ChouJiang u = new ChouJiang(id, cjname,cjcreatetime,cjupdatetime);
				list.add(u);
			}
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			WokeConnection.closeMySql(con, st, rs);
		}
		return list;
	}
	
	public int selectLendSize(){
		Connection conn = WokeConnection.getConnection();
		PreparedStatement pst=null;
		ResultSet rs=null;
		try {
			 pst = conn.prepareStatement("select count(*) from choujiang");
			 rs = pst.executeQuery();
			if(rs.next()){
				int pagecount = rs.getInt(1);
				return pagecount;
			}
			return 0;
		} catch (Exception e) {

			// TODO Auto-generated catch block

			e.printStackTrace();

			return 0;

		}finally{
			WokeConnection.closeMySql(conn, pst, rs);
		}
	}
	
	public List<ChouJiang> selectLend(int pageNow, int pageSize){

		List<ChouJiang> list = new ArrayList<ChouJiang>();
		Connection conn = WokeConnection.getConnection();
		PreparedStatement pst=null;
		ResultSet rs=null;
		
		try {
			int startIndex =0;
			int endIndex = pageSize;
			String sql = "select  * from choujiang ";
			String sw = " where " + " 1=1 " + " order by id Desc ";
			// sql +=" where " + strWhere ;
			if (pageNow > 1) {
				startIndex = pageSize * (pageNow - 1);
				// endIndex = pageSize * currentPage;
			}
			sql += sw + " limit " + startIndex + "," + endIndex;
			
			 pst= conn.prepareStatement(sql);
			 rs= pst.executeQuery();
			while(rs.next()){
				ChouJiang s= new ChouJiang();
				s.setId(rs.getInt(1));
				s.setCjname(rs.getString(2));
				s.setCjcreatetime(rs.getString(3));
				s.setCjupdatetime(rs.getString(4));
				list.add(s);
			}
			return list;
		} catch (Exception e) {

			// TODO Auto-generated catch block

			e.printStackTrace();

			return null;

		}finally{

			WokeConnection.closeMySql(conn, pst, rs);

		}

	}

	
}
